#from type import *
#from common import *
#from dictionary import *
#from meta import *
from monolithic import *
import monolithic

#class TypeLibrarySingleton(object):
#    @classmethod
#    def getDefault(cls):
#        if not hasattr(cls, '_lib'):
#            cls._lib = lib = TypeLibrary()
#            type_core_init( lib )
#            dictionary_types_init( lib )
#            bind_common( lib )
#        return cls._lib

class TypeReaderAuto(object):
    def __init__(self, constructor):
        self.constructor = constructor

    def read(self, instream, element):
        return monolithic._py_type_reader_auto( instream, element, self.constructor )



class ReferenceHolder(object):
    refs = []


def set_object( stack, element, obj ):
    ReferenceHolder.refs.append( obj )
    stack.set_ptr_( element, toVoid(obj) )

TypeStack.set_object = set_object


def get_object( item ):
    name = item.type.name.name

    #print name
    
    if name == UInt8.name.name:
        return item.data.u8
    if name == SInt8.name.name:
        return item.data.s8

    if name == UInt16.name.name:
        return item.data.u16
    if name == SInt16.name.name:
        return item.data.s16

    if name == UInt32.name.name:
        return item.data.u32
    if name == SInt32.name.name:
        return item.data.s32

    if name == UInt64.name.name:
        return item.data.u64
    if name == SInt64.name.name:
        return item.data.s64


    if name == U8Ascii.name.name:
        string = toString( item.data.ptr )
        #print string
        return string

    if name == U8Boolean.name.name:
        return item.data.u8 == 1

    if name == U32Utf8.name.name:
        raise NotImplementedError('Python equivalent of U32UTF8 not yet implemented')

    if name == U16Binary.name.name:
        raise NotImplementedError('Python equivalent of U16Binary not yet implemented')

    if name == U32Binary.name.name:
        raise NotImplementedError('Python equivalent of U32Binary not yet implemented')


    print '!Unknown Type, trying python object'
    return py_convert_to_python_object(item.data.ptr)

TypeItem.get_object = get_object
